import {AxdrType} from "./AxdrType";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {Buffer} from "buffer";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";


export class AxdrBoolean implements AxdrType {


    val: boolean = false;
    dataCode: Buffer | null = null;

    constructor() {
    }

    set_Val(val: boolean) {
        this.val = val;
    }

    set_dataCode(dataCode: Buffer) {
        this.dataCode = dataCode;
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess)
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    encode(output: ReverseByteArrayOutputStream): number {
        if (this.dataCode != null) {
            output.writeByte(this.dataCode);
        } else if (this.val) {
            output.write(0x01);
        } else {
            output.write(0x00);
        }
        return 1;
    }

    decode(input: ReverseByteArrayInputStream): number {
        if (input.available() == 0) {
            return 0;
        }
        this.val = input.read() != 0x00;
        return 1;
    }

    getVal(): boolean {
        return this.val;
    }

    toString(): string {
        return String(this.val);
    }


}
